package com.biglead.mini.controller;

import com.biglead.common.pojo.CommonResult;
import com.biglead.mini.pojo.CartProduct;
import com.biglead.mini.pojo.CartPromotionItem;
import com.biglead.mini.pojo.OmsCartItem;
import com.biglead.mini.service.OmsCartItemService;
import com.biglead.mini.service.UmsMemberService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 购物车管理Controller
 */
@Controller
@Api(tags = "OmsCartItemController")
@RequestMapping("/cart")
public class CartItemController {
    @Autowired
    private OmsCartItemService omsCartItemService;

    @ApiOperation("添加商品到购物车")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult add(@RequestHeader Long userId, @RequestBody OmsCartItem cartItem) {
        boolean count = omsCartItemService.add(cartItem, userId);
        if (count) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    @ApiOperation("获取当前会员的购物车列表")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<List<OmsCartItem>> list(@RequestHeader Long userId) {
        List<OmsCartItem> cartItemList = omsCartItemService.list(userId);
        return CommonResult.success(cartItemList);
    }

    @ApiOperation("获取当前会员的购物车列表,包括促销信息")
    @RequestMapping(value = "/list/promotion", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<List<CartPromotionItem>> listPromotion(@RequestHeader Long userId, @RequestParam(required = false) List<Long> cartIds) {
        List<CartPromotionItem> cartPromotionItemList = omsCartItemService.listPromotion(userId, cartIds);
        return CommonResult.success(cartPromotionItemList);
    }

    @ApiOperation("修改购物车中指定商品的数量")
    @RequestMapping(value = "/update/quantity", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult updateQuantity(
            @RequestHeader Long userId,
            @RequestParam Long id,
            @RequestParam Integer quantity) {
        boolean count = omsCartItemService.updateQuantity(id, userId, quantity);
        if (count) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    @ApiOperation("获取购物车中指定商品的规格,用于重选规格")
    @RequestMapping(value = "/getProduct/{productId}", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<CartProduct> getCartProduct(@PathVariable Long productId) {
//        CartProduct cartProduct = omsCartItemService.getCartProduct(productId);
        return CommonResult.success(null);
    }

    @ApiOperation("修改购物车中商品的规格")
    @RequestMapping(value = "/update/attr", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult updateAttr(@RequestBody OmsCartItem cartItem) {
//        int count = omsCartItemService.updateAttr(cartItem);
//        if (count > 0) {
//            return CommonResult.success(count);
//        }
        return CommonResult.failed();
    }

    @ApiOperation("删除购物车中的指定商品")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult delete(@RequestHeader Long userId,@RequestParam("ids") List<Long> ids) {
        boolean count = omsCartItemService.delete(userId, ids);
        if (count) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    @ApiOperation("清空当前会员的购物车")
    @RequestMapping(value = "/clear", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult clear(  @RequestHeader Long userId) {
        boolean count = omsCartItemService.clear(userId);
        if (count) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }
}
